package com.hourse.app.utils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Random;

public class DateUtil {


    public static String ptn_yyyyMMDD = "yyyyMMdd";
    public static String ptn_8 = "yyyyMMdd";
    public static String ptn_10 = "yyyy-MM-dd";
    public static String ptn_14 = "yyyyMMddHHmmss";
    public static String ptn_18 = "yyyy-MM-dd HH:mm:ss";
    public static String ptn_17 = "yyyyMMddHHmmssSSS";

    /**
     * @描述：【Unix时间戳转换成指定格式日期字符串】
     * @作者：lichenguang
     * @时间: 2022/1/21 20:21
     */
    public static String TimeStamp2Date(String timestampString, String pattern) {
        Long timestamp = Long.parseLong(timestampString);
        String date = new SimpleDateFormat(pattern, Locale.CHINA).format(new Date(timestamp));
        return date;
    }

    /**
     * yyyyMMdd
     *
     * @param pattern
     * @return
     */
    public static String DateToString(String pattern) {
        return new SimpleDateFormat(pattern).format(new Date());
    }

    public static String DateToString(Date date, String pattern) {
        return new SimpleDateFormat(pattern).format(date);
    }

    public static int compareDays(Date mainDate, Date secondDate) {
        Long n = ((mainDate.getTime() - secondDate.getTime()) / 86400000 + 1);
        return n.intValue();
    }

    public static int compareMonthes(Date date, Date stringToDate) {

        String year01 = DateToString(date, "yyyy");
        String year02 = DateToString(date, "yyyy");

        String month01 = DateToString(date, "MM");
        String month02 = DateToString(date, "MM");

        return (Integer.valueOf(year02) - Integer.valueOf(year01)) * 12 + (Integer.valueOf(year02) - Integer.valueOf(year01));


    }

    public static int compareYears(Date date, Date stringToDate) {
        String year01 = DateToString(date, "yyyy");
        String year02 = DateToString(date, "yyyy");

        return (Integer.valueOf(year02) - Integer.valueOf(year01));
    }

    public static Date StringToDate(String createTime) {

        try {
            return new SimpleDateFormat("yyyy-MM-dd").parse(createTime);
        } catch (ParseException e) {
            //e.printStackTrace();
        }

        try {
            return new SimpleDateFormat(ptn_18).parse(createTime);
        } catch (ParseException e) {
            // e.printStackTrace();
        }


        try {
            return new SimpleDateFormat(ptn_14).parse(createTime);
        } catch (ParseException e) {
            //e.printStackTrace();
        }

        try {
            return new SimpleDateFormat(ptn_17).parse(createTime);
        } catch (ParseException e) {
            //e.printStackTrace();
        }

        try {
            return new SimpleDateFormat(ptn_8).parse(createTime);
        } catch (ParseException e) {
            // e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取当前时间 的 00:00:00
     */
    public static Date curtDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        try {
            return format.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取当前日期
     *
     * @param pattern 日期格式
     * @return
     */
    public static String getCurrentTime(String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(new Date());
    }


    /**
     * 生成指定位数的随机数
     *
     * @param length
     * @return
     */
    public static String getRandom(int length) {
        String val = "";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            val += String.valueOf(random.nextInt(10));
        }
        return val;
    }

    /*
     * 该方法用于比较两个用字符串表示的日期
     * param strDate1,strDate2 要求的格式为'yyyy-mm-dd'
     * 返回值为两个日期相差的天数
     */
    public static int compareDate(String strDate1, String strDate2) {
        int returnVal = 0;

        try {
            Date date1 = getDateFromString(strDate1 + " 00:00:00");
            Date date2 = getDateFromString(strDate2 + " 00:00:00");
            returnVal = (int) ((date2.getTime() - date1.getTime()) / (1000 * 60 * 60 * 24));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return returnVal;
    }

    public static Date getDateFromString(String s) {
        Date returnDate = new Date();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(ptn_18);
            returnDate = sdf.parse(s);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return returnDate;
    }

    public static Date getDateFrom(String s,String ptn) {
        Date returnDate = new Date();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(ptn);
            returnDate = sdf.parse(s);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return returnDate;
    }

    public static void main(String[] args) {
        // int i = compareDate("2021-01-26", "2021-01-01");
        // System.out.println(i);
        //-25

        System.out.println(new BigDecimal(17000).compareTo(new BigDecimal(18000)));

    }

    public static String getLastMonth(String pattern) {

        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -1);

        //得到前一个月
        String start = format.format(c.getTime());
        System.out.println(start);

        return start;
    }
}
